# -*- coding: utf-8 -*-
"""
Created on Wed Apr 22 14:39:04 2020

@author: yu
"""

import random
from tkinter import _flatten
class od(): 
    def __init__(self,seedNum,totVeh):
        
        self.seedNum=seedNum
        self.totVeh=totVeh
    
    def getO(self):
    # 伪随机，能选中1或2个数字
        randomO=self.totVeh/3
        loopT=0
        randomT=self.totVeh/3
        startL=[]
        childPool=[]
        for i in range(1,self.totVeh+1):
            childPool.append(str(i))
        random.seed(self.seedNum)
        random.shuffle(childPool)
        while True:
            
            loopT+=1
            if randomO!=0  and randomT!=0:
                carNum=random.sample([1,2],1)
            elif randomO==0 and randomT!=0:
                carNum=[2]
            elif randomO!=0 and randomT==0:
                carNum=[1]
            else:
                break
            if carNum==[1]:
                randomO-=1
            else:
                randomT-=1
            #print('loopT',loopT,'randomNum',carNum)
            ranRow=[]
            #print('childPool',childPool)
            for _ in range(carNum[0]):
                ranRow.append(childPool[0])
                childPool.remove(childPool[0])
            #print('childPool after remove',childPool)
            while len(ranRow)<3:
                ranRow.append('0')
            random.shuffle(ranRow)
            #print('ranRow',ranRow)
            startL.append(ranRow)
            
            startL=list(_flatten(startL))
        return startL
    @staticmethod
    def noneZero(a_list):
        #not_zero=0
        childrenL=[]
        for i in a_list:
            if i!='0':
                #not_zero+=1
                childrenL.append(i)
        return childrenL
    
    def getD(self):
        startL=self.getO()
        pool={}
        endL=[]
        for _ in range(int(self.totVeh/3)):
            for i in startL:
                pool[i]=0
            # select next 3 rows of cars
            nineC=startL[:9]
            #print('next_child_no: 9','nineC',nineC)
            
            # from 9 positions, random select 3 cars
            numC=self.noneZero(nineC)
            #print(numC)
            nextR=[]
            totOption=3
            
            # record select times # if in the next row, delete
            for i in numC:
                pool[i]+=1
                if pool[i]==2:
                    nextR.append(pool[i])
                    totOption-=1
                    numC.remove(pool[i])
            
            # shuffle
            nextR.append(random.sample(numC,totOption))
            nextR=list(_flatten(nextR))   
            for j in nextR:
                startL.remove(j)
            # delete the 000 as during the sorting, it will also be deleted
            remainRow=len(startL)//3
            count=0
            for i in range(0,remainRow):
                if startL[3*i]=='0' and startL[3*i+1]=='0' and startL[3*i+2]=='0':
                    count+=1
                    startL=["0","0","0"]+startL[:3*i]+startL[3*i+3:] # 补齐长度
            startL=startL[3*count:]
            
            # get end list
            random.seed(1)
            random.shuffle(nextR)
            endL.append(nextR)
            endL=list(_flatten(endL))    
        return endL

#print('startL',od().getO(),'endL',od().getD())







